<?php

namespace app\home\controller;

use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class Company extends BaseController
{
    protected $table = 'company';
    protected $title = '多企业';

    /**
     * @return void
     */
    public function index()
    {
        $param = $this->param;
        if (request()->isAjax()) {

            $where[] = ['status', '>', -1];

            foreach (['p_name', 'u_username','i_orderNum'] as $i) {
                if (isset($param[$i]) && $param[$i]) {
                    $k = str_replace('_', '.', $i);
                    $where[] = [$k, 'like', '%' . $param[$i] . '%'];
                }
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->where($where)
                ->order('id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }

}

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param=$this->param;
            $param['update_time']=time();
            if ($param['id'] > 0) {
                Db::name($this->table)->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param,$this->title);
            } else {
                $param['create_time'] = time();
                $rid = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                add_log('add', $rid, $param,$this->title);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            return view('form');
        }

}
    public function doCheck()
    {
        $param = get_params();
        $param['update_time']=time();
        $res = Db::name($this->table)->strict(false)->field('id,status,update_time')->update($param);
        if ($res) {
            if($param['status'] == 2){
                add_log('disable', $param['id'], $param,$this->title);
            }
            else if($param['status'] == 1){
                add_log('recovery', $param['id'], $param,$this->title);
            }
            return to_assign();
        }
        else{
            return to_assign(0, '操作失败');
        }
    }
    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            try {
                Db::name($this->table)->where('id','in',$id)->update(['status'=>-1,'deleted'=>time()]);
                add_log('delete', $id, request()->param(),$this->title);
            }catch (\Exception $e){
                return to_assign(1, "删除失败".$e->getMessage());
            }
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "错误的请求");
        }
    }
}